package tetris;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author Fermin Jimenez & Javier Sigüenza
 */
public class PiezaZ extends Pieza {

    Tetris t;

    public PiezaZ(int x, int y, Tetris t) {
        super.inicio(x, y, t);
        this.t = t;
    }

    public void izquierda() {

        if (coordenadax - 30 >= 50 && !detectarColisionIz()) {
            coordenadax -= 30;
        }

    }

    public void derecha() {

        if (coordenadax + 90 <= 330 && !detectarColisionDer()) {
            coordenadax += 30;
        }
    }

    public void pintar(Graphics g) {
        g.setColor(Color.pink);
        g.fillRect(coordenadax, coordenaday, 30, 30);
        g.fillRect(coordenadax + 30, coordenaday, 30, 30);
        g.fillRect(coordenadax + 30, coordenaday - 30, 30, 30);
        g.fillRect(coordenadax + 60, coordenaday - 30, 30, 30);
    }

    public boolean detectarColision() {
        boolean detc = false;
        int nx, ny;

        nx = coordenadax - 50;
        ny = coordenaday - 20;

        nx = nx / 30;//3
        ny = ny / 30;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                if (t.arraytetris[nx][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx + 1][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx + 1][ny - 1] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx + 2][ny - 1] != 0) {
                    detc = true;
                }
            }
        }
        return detc;
    }

    public void guardarEnTetris() {
        int nx, ny;

        nx = coordenadax - 50;
        ny = coordenaday - 50;

        nx = nx / 30;//1
        ny = ny / 30;//19
        System.out.println(nx + "   " + ny);
        t.arraytetris[nx][ny] = 2;
        t.arraytetris[nx + 1][ny] = 2;
        t.arraytetris[nx + 1][ny - 1] = 2;
        t.arraytetris[nx + 2][ny - 1] = 2;

        t.piezaAleatoria();

    }
    
    public boolean detectarColisionIz(){
        boolean detc = false;
        int nx, ny;

        nx = coordenadax - 50;
        ny = coordenaday - 20;

        nx = nx / 30;//3
        ny = ny / 30;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                if (t.arraytetris[nx-1][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx][ny - 1] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx + 1][ny - 1] != 0) {
                    detc = true;
                }
            }
        }
        return detc;
    }

    public boolean detectarColisionDer() {
        boolean detc = false;
        int nx, ny;

        nx = coordenadax - 50;
        ny = coordenaday - 20;

        nx = nx / 30;//3
        ny = ny / 30;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                if (t.arraytetris[nx+2][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx + 3][ny] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx+2][ny - 1] != 0) {
                    detc = true;
                }
                if (t.arraytetris[nx+2][ny - 2] != 0) {
                    detc = true;
                }
            }
        }
        return detc;
    }
}
